﻿using Sw.Hospital.HealthExaminationSystem.ApiProxy;
using Sw.Hospital.HealthExaminationSystem.ApiProxy.Api.Controllers;
using Sw.Hospital.HealthExaminationSystem.Application.CusReg.Dto;
using Sw.Hospital.HealthExaminationSystem.Comm;
using Sw.Hospital.HealthExaminationSystem.WebApi.Api.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HealthExaminationSystem.AutoExportReport
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public Form1(string gid)
        {
            this.gid = gid;
            InitializeComponent();
            
        }
        string gid = "";
        

        /// <summary>
        /// 发送报告短信
        /// </summary>
        [System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptions]
        [System.Security.SecurityCritical]
        private void SendReportMessage()
        {
            //Thread.Sleep(1000 * 30);
            //return;
            try
            {
                string strcon = ConfigurationManager.AppSettings["SqlDBConn"];
                string path = ConfigurationManager.AppSettings["PDFPath"]; 
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                string strnewpath = path + "\\" + gid;
                var printReport = new PrintReportNew();
                var cusNameInput = new CusNameInput { Id = Guid.Parse(gid) };
                printReport.cusNameInput = cusNameInput;
                //printReport.Print(false, strnewpath, "");
                printReport.Print(false, strnewpath, "", "0", 2);
                GC.Collect();
                //if (!string.IsNullOrEmpty(Copypath) && Directory.Exists(Copypath))
                //{
                //    File.Copy(strnewpath, Copypath);
                //}

                var strsqlUp = @"update TjlCustomerRegs set WXExportSate=2  where id='" + gid + "'";
                //var strsqlUp = @"update TjlCustomerRegs set ExportSate=2  where id='" + dt.Rows[i]["Id"].ToString() + "'";

                SQLHelper.UpdateSQl(strsqlUp, strcon);

                //var strsqlIN = @"insert into ReportList([CustomerBM],[Indate]) values ('" + cusBM + "',GETDATE())";
                //SQLHelper.UpdateSQl(strsqlIN, strcon);
                GC.Collect();
                //MessageBox.Show(strsqlIN);

                //Num = Num + 1;
                ////每30个休息2分钟
                //if (Num >= 30)
                //{
                //    Thread.Sleep(120000);
                //    Num = 0;
                //}
                //WriteTxt(memoEditRetMsg.Text);
            }
            catch (Exception ex)
            {

            }

            finally
            {
                GC.Collect();
                Application.Exit();
            }
        }



    

       

        public static void ClearMemory()
        {
            GC.Collect();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var login = new AccountController();
            var loginModel = new LoginModel
            {
                TenancyName = "test",
                UsernameOrEmailAddress = "admin",
                Password = "123qwe"
            };
            try
            {
                login.Authenticate(loginModel);
                //自助机
                //Application.Run(new Startup());

                //微信导报告
                //这句必须加上，否则backgroundWorker1处理不了控件
                Control.CheckForIllegalCrossThreadCalls = false;
                
                SendReportMessage();


            }
            catch (UserFriendlyException ee)
            {
                //MessageBox.Show(ee.Description, ee.Code.ToString(), ee.Buttons, ee.Icon);
            }
        }
    }
}
